package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.Country;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class StationFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField stationId = new TextField("Station id");
	final TextField code = new TextField("Code");
	final TextField name = new TextField("Name");
	NativeSelect status = new NativeSelect("Status");
	NativeSelect country = new NativeSelect("Country");

	public StationFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("stationId".equals(propertyId)) {
			stationId.setRequired(true);
			stationId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return stationId;
		} if ("code".equals(propertyId)) {
			code.setRequired(true);
			code.setWidth(Constants.COMMON_FIELD_WIDTH);
			code.setRequiredError("Code is required");
			code.setNullRepresentation("");
			return code;
		} if ("name".equals(propertyId)) {
			name.setRequired(true);
			name.setWidth(Constants.COMMON_FIELD_WIDTH);
			name.setRequiredError("Name is required");
			name.setNullRepresentation("");
			return name;
		} if ("stationStatus".equals(propertyId)) {
			try{
				status = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<StationStatus> container = JPAContainerFactory.make(StationStatus.class, app.PERSISTENCE_UNIT);
				status.setContainerDataSource(container);
			}
			status.setWidth(Constants.COMMON_FIELD_WIDTH);
			status.setNullSelectionAllowed(false);
			status.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			status.setRequired(true);
			status.setRequiredError("Status is required");
			return status;
		} if ("country".equals(propertyId)) {
			try{
				country = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Country> container = JPAContainerFactory.make(Country.class, app.PERSISTENCE_UNIT);
				country.setContainerDataSource(container);
			}
			country.setWidth(Constants.COMMON_FIELD_WIDTH);
			country.setNullSelectionAllowed(false);
			country.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			country.setRequired(true);
			country.setRequiredError("Country is required");
			return country;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
